package cn.ibizlab.odoo.core.odoo_mail.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imail_message_subtype;
import cn.ibizlab.odoo.core.odoo_mail.domain.Mail_message_subtype;
import cn.ibizlab.odoo.core.odoo_mail.filter.Mail_message_subtypeSearchContext;
import cn.ibizlab.odoo.core.odoo_mail.service.IMail_message_subtypeService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[消息子类型] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Mail_message_subtypeServiceImpl implements IMail_message_subtypeService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imail_message_subtypeClientService mail_message_subtypeClientService;

    @Override
    public void createBatch(List<Mail_message_subtype> mail_message_subtypes, int batchSize) {

                        //mail_message_subtypeClientService.createBatch(mail_message_subtypes,batchSize) ;

    }

    @Override
    public void updateBatch(List<Mail_message_subtype> mail_message_subtypes, int batchSize) {

        //mail_message_subtypeClientService.updateBatch(mail_message_subtypes,batchSize) ;

    }

    @Override
    public Mail_message_subtype get(Integer id) {
        Imail_message_subtype clientModel = mail_message_subtypeClientService.createModel();
        clientModel.setId(id);
        mail_message_subtypeClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Mail_message_subtype get(Mail_message_subtype mail_message_subtype) {
        Mail_message_subtype retmail_message_subtype = this.get(mail_message_subtype.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmail_message_subtype.getClass(), mail_message_subtype.getClass(), false);
        copier.copy(retmail_message_subtype, mail_message_subtype, null);
        return mail_message_subtype ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //mail_message_subtypeClientService.removeBatch(mail_message_subtypes) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imail_message_subtype clientModel = mail_message_subtypeClientService.createModel();
        clientModel.setId(id);
        mail_message_subtypeClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean create(Mail_message_subtype mail_message_subtype) {
        boolean bOk = false ;
        Imail_message_subtype clientModel = this.convert2Model(mail_message_subtype,null);
        mail_message_subtypeClientService.create(clientModel) ;
        this.convert2Domain(clientModel,mail_message_subtype);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean update(Mail_message_subtype mail_message_subtype) {
        boolean bOk = false ;
        Imail_message_subtype clientModel = this.convert2Model(mail_message_subtype,null);
        mail_message_subtypeClientService.update(clientModel) ;
        this.convert2Domain(clientModel,mail_message_subtype) ;
        bOk = true;
        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Mail_message_subtype> searchDefault(Mail_message_subtypeSearchContext context) {
        List<Mail_message_subtype> list = new ArrayList<Mail_message_subtype>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_message_subtype> clientModelList = mail_message_subtypeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_message_subtype clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_message_subtype>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据消息子类型查询
     */
    @Override
    public Page<Mail_message_subtype> selectByOdoo_parent(Integer id){
        Mail_message_subtypeSearchContext context = new Mail_message_subtypeSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_message_subtype> list = new ArrayList<Mail_message_subtype>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_message_subtype> clientModelList = mail_message_subtypeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_message_subtype clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_message_subtype>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mail_message_subtype> selectByOdoo_create(Integer id){
        Mail_message_subtypeSearchContext context = new Mail_message_subtypeSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_message_subtype> list = new ArrayList<Mail_message_subtype>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_message_subtype> clientModelList = mail_message_subtypeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_message_subtype clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_message_subtype>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mail_message_subtype> selectByOdoo_write(Integer id){
        Mail_message_subtypeSearchContext context = new Mail_message_subtypeSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_message_subtype> list = new ArrayList<Mail_message_subtype>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_message_subtype> clientModelList = mail_message_subtypeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_message_subtype clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_message_subtype>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Imail_message_subtype convert2Model(Mail_message_subtype domain , Imail_message_subtype model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = mail_message_subtypeClientService.createModel() ;
        }

         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getRelation_fieldDirtyFlag())
            model.setRelation_field(domain.getRelation_field());
         if(domain.getInternalDirtyFlag())
            model.setInternal(domain.getInternal());
         if(domain.getSequenceDirtyFlag())
            model.setSequence(domain.getSequence());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getIbizdefaultDirtyFlag())
            model.setIbizdefault(domain.getIbizdefault());
         if(domain.getDescriptionDirtyFlag())
            model.setDescription(domain.getDescription());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getHiddenDirtyFlag())
            model.setHidden(domain.getHidden());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getRes_modelDirtyFlag())
            model.setRes_model(domain.getRes_model());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getParent_id_textDirtyFlag())
            model.setParent_id_text(domain.getParent_id_text());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getParent_idDirtyFlag())
            model.setParent_id(domain.getParent_id());
        return model ;
    }

    public Mail_message_subtype convert2Domain( Imail_message_subtype model ,Mail_message_subtype domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Mail_message_subtype();
        }

        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getRelation_fieldDirtyFlag())
            domain.setRelation_field(model.getRelation_field());
        if(model.getInternalDirtyFlag())
            domain.setInternal(model.getInternal());
        if(model.getSequenceDirtyFlag())
            domain.setSequence(model.getSequence());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getIbizdefaultDirtyFlag())
            domain.setIbizdefault(model.getIbizdefault());
        if(model.getDescriptionDirtyFlag())
            domain.setDescription(model.getDescription());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getHiddenDirtyFlag())
            domain.setHidden(model.getHidden());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getRes_modelDirtyFlag())
            domain.setRes_model(model.getRes_model());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getParent_id_textDirtyFlag())
            domain.setParent_id_text(model.getParent_id_text());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getParent_idDirtyFlag())
            domain.setParent_id(model.getParent_id());
        return domain ;
    }


}
